Dir.chdir File.dirname(__FILE__) + "/.."
namespace :wiki do
  directory 'tmp/wiki'
  
  desc "Checkout the Wiki to tmp/wiki"
  task :checkout => "tmp/wiki" do
    sh 'svn co https://space-trader.googlecode.com/svn/wiki tmp/wiki'
  end
  
  desc "Update the wiki in tmp/wiki"
  task :update do
    sh 'svn up tmp/wiki'
  end
  
  desc "Either checkout or update the wiki, depending on the existance of tmp/wiki"
  task :checkout_or_update do
    method = File.exists?('tmp/wiki') ? "update" : "checkout"
    Rake::Task["wiki:#{method}"].invoke
  end
  
  desc "Commit any changes to the wiki"
  task :commit do
    sh 'svn commit -m "Automatic update from Rake script" tmp/wiki'
  end
  
  desc "Clean up the local wiki checkout"
  task :clean do
    FileUtils.rm_rf 'tmp/wiki' if File.exists?('tmp/wiki')
  end
  
  desc "Upload specdocs to Specs page of wiki"
  task :upload_specs => ["wiki:checkout_or_update", "specs:doc"] do
    specs = File.read("doc/specs.txt")
    specs.gsub!('\n', '\n\n')
    specs = "#summary Specification Results\n#labels API,Docs,Generated\n" << specs
    
    wiki_page = File.open("tmp/wiki/Specs.wiki", "w")
    wiki_page.write(specs)
    wiki_page.close
    
    Rake::Task["wiki:commit"].invoke
  end
end